using System;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Data.Common;
using System.Data;
using System.Data.SqlTypes;

namespace uk.co.tonyditchfield.tort.utility
{
    /// <summary>
    /// Class to allow native type coversion into specific parameter types
    /// rather than relying on DbTypes completely
    /// </summary>
    internal class ParameterBuilder
    {
        internal Object GetParameter(string parameterName, object columnValue, Type nativeType)
        {
            DbParameter param = null;
            if (database.DBClient.Connection().GetType() == typeof(SqlConnection))
            {
                param = GetSQLParameter(parameterName, columnValue, nativeType);
            }
            else if (database.DBClient.Connection().GetType() == typeof(OracleConnection))
            {
                param = GetOracleParameter(parameterName, columnValue, nativeType);
            }
            return param;
        }

        private SqlParameter GetSQLParameter(string parameterName, object columnValue, Type nativeType)
        {
            SqlParameter param = new SqlParameter();
            param.ParameterName = parameterName;
            param.SqlDbType = new utility.conversion.DbTypeConverter().ConvertToSqlDbType(nativeType);
            param.Value = columnValue == null ? DBNull.Value : columnValue;
            return param;
        }

        private OracleParameter GetOracleParameter(string parameterName, object columnValue, Type nativeType)
        {
            OracleParameter param = new OracleParameter();
            param.ParameterName = parameterName;
            param.OracleType = new utility.conversion.DbTypeConverter().ConvertToOracleDbType(nativeType);
            return param;
        }
        // TESTING!!
        internal SqlParameter GetIdentity(string columnName, string parameterName, object columnValue, Type nativeType)
        {
            SqlParameter param = new SqlParameter();
            param.Direction = ParameterDirection.Output;
            param.ParameterName = parameterName;
            param.SqlDbType = new utility.conversion.DbTypeConverter().ConvertToSqlDbType(nativeType);
            param.SourceColumn = columnName;
            return param;
        }
    }
}